Boolean Values and Conditional Operators

There are two boolean values, true and false. Boolean values can be used to decide which code to execute in a program. Matlab, like many programming languages, uses numbers to represent boolean values. The number 0 (zero) is used to represent false and the number 1 (one) is used to represent true.

There are two types of conditional operators, relational and logical. Conditional operators are also known as boolean operators. The result of each type of conditional operation is a boolean value. The type of the operands of an expression determine which type of conditional operator can and should be used.

If the operands are both numbers, use relational operators. If the operands are boolean values, use logical operators. It is usually an error to compare numbers to boolean values and vice versa. Complicated boolean expressions can be constructed using logical operators, relational operators, or a combination of both.

Relational Operators

Relational operators are used to compare numeric values to each other. A relational operator answers a yes-or-no question about the relationship between two values. The result of an expression with a relational operator is a boolean value (i.e. either true or false).

In Matlab, the relational operators are:

Operator Description               Example        Value (assuming x = 10, y = 24)
---------------------------------------------------------------------------------
  ==     equal to                  x == 12        false
  ~=     not equal to              y ~= 20        true
  <      less than                 x < y          true
  <=     less than or equal to     y <= x         false
  >      greater than              x + 20 > y     true
  >=     greater than or equal to  x + y >= 57    false

Boolean Logic Operators

Boolean logic operators are used to compare boolean values (true or false) to each other. There are three logical operators, AND, OR, and NOT.

NOT
The NOT operation is a unary operation, that is, it operates on just one value (or operand). The meaning of the logical NOT operation is to flip the boolean value, from true to false or from false to true. In Matlab, the NOT operation is represented by the ~ (tilde) character.

AND
The AND operation is a binary logical operator that compares two operands and returns a result of true, if and only if both operands are true. The AND operation returns false if either operand is false. In Matlab, the symbol & represents the AND operation.

OR
The OR operation is a binary logical operator that compares two operands and returns a result of true, if either operand is true. The OR operation returns false if both operands are false. In Matlab, the symbol | (pipe) represents the OR operation.

Operator Precedence

Here are the results of help precedence in Matlab to show the precedence of Matlab's operators.

>> help precedence
 PRECEDENCE Operator Precedence in MATLAB.
 
    MATLAB has the following precedence for the built-in operators when
    evaluating expressions (from highest to lowest):
 
       1. transpose (.'), power (.^), complex conjugate
          transpose ('), matrix power (^)
 
       2. unary plus (+), unary minus (-), logical negation (~)
 
       3. multiplication (.*), right division (./), left
          division (.\), matrix multiplication (*), matrix right 
          division (/), matrix left division (\)
 
       4. addition (+), subtraction (-)
 
       5. colon operator (:)
 
       6. less than (<), less than or equal to (<=), greater than
          (>), greater than or equal to (>=), equal to (==), not
          equal to (~=)
 
       7. element-wise logical AND (&)
 
       8. element-wise logical OR (|)
 
       9. short-circuit logical AND (&&)
 
      10. short-circuit logical OR (||)
 

The short-circuit logical AND and short-circuit logical OR operators are used when you wish to skip the execution of later parts of a complex operation if the end result of the operation can be known from the earlier parts.